﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BOM.dao;
using BOM.entity;
using BOM.BaseData;

namespace BOM.PriceBill
{
    public partial class PriceBillEntryForm : Form
    {
        public static int Rows = 0; //判断把 记录写到哪一行！

        public static bool EditOrNew = false;//false表示新增 true表示修改

        public static int Revisefid = 0;
        private PriceBillForm parentUI;
        public PriceBillEntryForm()
        {
            InitializeComponent();
            parentUI = (PriceBillForm)this.Owner;
        }
        //窗体的加载事件
        private void PriceBillEntryForm_Load(object sender, EventArgs e)
        {
            parentUI = (PriceBillForm)this.Owner;
            try
            {
                if (EditOrNew == false)
                {
                    //新增状态
                    DataTable table = PriceBillEntryFormNew.SelectMaterialgroup();
                    if (table != null)
                    {
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            string str = table.Rows[i]["fname"].ToString();
                            cbMaterialGroup.Items.Add(str);
                        }
                    }
                }
                //编辑状态
                else if (EditOrNew == true)
                {
                    DataTable table = PriceBillEntryFormNew.SelectMaterialgroup();
                    if (table != null)
                    {
                        for (int i = 0; i < table.Rows.Count; i++)
                        {
                            string str = table.Rows[i]["fname"].ToString();
                            cbMaterialGroup.Items.Add(str);
                        }
                    }
                    cbMaterialGroup.Enabled = false;
                    TemporaryValue value = new TemporaryValue();
                    int materialGroupId = value.FmaterialGroupId;
                    DataTable tb1 =  MaterialgroupNew.QuryfidMaterialgroup(materialGroupId);
                    if (tb1 != null)
                        cbMaterialGroup.Text = tb1.Rows[0]["fname"].ToString();
                    cbMaterial.Text = value.Fmaterial;
                    txtSize.Text = value.Fsize;
                    textUnit.Text = value.Funit;
                    txtPrice.Text = value.Fpric.ToString();
                    txtQty.Text = value.Fqty.ToString();
                    textAmount.Text = value.Famout.ToString();
                    txtDecription.Text = value.Fdescription;
                   // DataTable tablefid = PriceBillEntryFormNew.SelectMateriGroupsFid(txtQty.Text, txtPrice.Text, CustomerList.XuanZID);
                   // if (tablefid != null)
                   // {
                    Revisefid = value.Fid;// Convert.ToInt32(tablefid.Rows[0]["pricebillentry.fid"]);
                   // }
                }
            }
            catch (Exception e9)
            {
                MessageBox.Show(e9.Message);
            }
        }
        //当combox的值发生改变时发生的事件
        private void cbMaterialGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbMaterial.Items.Clear(); 
            cbMaterial.Text = "";
            txtSize.Text = "";
            textUnit.Text = "";
            txtPrice.Text = "";
            txtQty.Text = "";
            textAmount.Text = "";
            try
            {
                string fname = cbMaterialGroup.Text;
                //MessageBox.Show(fname);
                DataTable table = PriceBillEntryFormNew.SelectfnameMaterialfid(fname);
                if (table != null)
                {
                    int fid = Convert.ToInt32(table.Rows[0]["fid"].ToString());
                    DataTable fnametable = PriceBillEntryFormNew.Selectfidmaterial(fid);
                    if (fnametable != null)
                    {
                        for (int i = 0; i < fnametable.Rows.Count; i++)
                        {
                            string str = fnametable.Rows[i]["fname"].ToString();
                            cbMaterial.Items.Add(str);
                        }
                    }
                }
            }
            catch (Exception e8)
            {
                MessageBox.Show(e8.Message);
            }
        }
        //确定按钮
        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtQty.Text.Trim() == "")
            {
                MessageBox.Show("数量是必须的", "提示");
            }
            else
            {
                try
                {
                    if (EditOrNew == false)
                    {
                        DataTable table = PriceBillEntryFormNew.SelectmaterialMaterialgroup(cbMaterial.Text, cbMaterialGroup.Text);
                        TemporaryValue value = new TemporaryValue();
                        value.Fmaterialid = Convert.ToInt32(table.Rows[0]["fid"].ToString());
                        DataTable table1 = PriceBillEntryFormNew.SelectfnameMaterialfid(cbMaterialGroup.Text);
                        value.Fmaterialgroup = cbMaterialGroup.Text;
                        value.FmaterialGroupId = Convert.ToInt32(table1.Rows[0]["fid"].ToString());
                        value.Fmaterial = cbMaterial.Text;
                        value.Fsize = txtSize.Text;
                        value.Fpric = float.Parse(txtPrice.Text);
                        value.Funit = textUnit.Text;
                        //设定数量存入数据库为四位小数
                        txtQty.Text = Math.Round(float.Parse(txtQty.Text), 4).ToString();
                        value.Fqty = float.Parse(txtQty.Text);
                        //value.Fqty = float.Parse(txtQty.Text);
                        //设定金额存入为四位小数
                        textAmount.Text = Math.Round(float.Parse(textAmount.Text), 4).ToString();
                        value.Famout = float.Parse(textAmount.Text);
                        value.Fdescription = txtDecription.Text;

                        PriceBillForm priceBillFrom = (PriceBillForm)this.Owner;
                        //加载表面积文本框
                        priceBillFrom.BasisMaterialarea(cbMaterial.Text, cbMaterialGroup.Text, float.Parse(txtQty.Text));
                        //加载周长文本框
                       // priceBillFrom.BasisMaterialperimeter(cbMaterial.Text, cbMaterialGroup.Text, float.Parse(txtQty.Text));


                        priceBillFrom.Transmitdatagview(Rows);


                        MessageBox.Show("新增行成功", "提示");
                        this.Close();
                    }
                    else if (EditOrNew == true)
                    {
                        DataTable table = PriceBillEntryFormNew.SelectmaterialMaterialgroup(cbMaterial.Text, cbMaterialGroup.Text);
                        TemporaryValue value = new TemporaryValue();
                        value.Fmaterialid = Convert.ToInt32(table.Rows[0]["fid"].ToString());
                        value.Fmaterialgroup = cbMaterialGroup.Text;
                        value.Fmaterial = cbMaterial.Text;
                        value.Fsize = txtSize.Text;
                        value.Fpric = float.Parse(txtPrice.Text);
                        value.Funit = textUnit.Text;
                        value.Fqty = float.Parse(txtQty.Text);
                        value.Famout = float.Parse(textAmount.Text);
                        value.Fdescription = txtDecription.Text;
                        DataGridView tb = parentUI.getDataGridView();
                        int rowIndex = tb.CurrentCell.RowIndex;
                        tb.Rows[rowIndex].Cells["materialid"].Value = value.Fmaterialid;
                        tb.Rows[rowIndex].Cells["material"].Value = value.Fmaterial;
                        tb.Rows[rowIndex].Cells["size"].Value = value.Fsize;
                        tb.Rows[rowIndex].Cells["unit"].Value = value.Funit;
                        tb.Rows[rowIndex].Cells["qty"].Value = value.Fqty;
                        tb.Rows[rowIndex].Cells["amount"].Value = value.Famout;
                        tb.Rows[rowIndex].Cells["description"].Value = value.Fdescription;
                        parentUI.setSumFieldValue();
                        /*  if (Revisefid != 0)
                            {
                                bool bol = PriceBillEntryFormNew.UpdatePricebillEntry(Revisefid);
                                if (bol == true)
                                {
                                    MessageBox.Show("修改成功", "提示");
                                    PriceBillForm priceBillFrom = (PriceBillForm)this.Owner;
                                    priceBillFrom.NewLoadingdatagridview();//重新加载datagridview数据
                                }
                            }
                         */
                    }
                }
                catch
                {
                    //MessageBox.Show(e6.Message);
                }
            }
        }
        //当combox的值发生改变时发生的事件
        private void cbMaterial_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable table = PriceBillEntryFormNew.SelectmaterialMaterialgroup(cbMaterial.Text, cbMaterialGroup.Text);
            if (table != null)
            {
                txtSize.Text = table.Rows[0]["fsize"].ToString();
                textUnit.Text = table.Rows[0]["funit"].ToString();
                txtPrice.Text = table.Rows[0]["fprice"].ToString();
                txtQty.Text = "";
                textAmount.Text = "";
                
            }
        }
        //成功验证控件后发生
        private void txtQty_Validated(object sender, EventArgs e)
        {
            if (txtPrice.Text.Trim() != "" && txtQty.Text.Trim()!="")
            {
                txtQty.Text = Math.Round(float.Parse(txtQty.Text), 4).ToString();
                //MessageBox.Show(txtQty.Text);
                float price = float.Parse(txtPrice.Text);
                float fo = float.Parse(txtQty.Text);
                float sum = price * fo;
                textAmount.Text = sum.ToString();
            }
        }
        //验证是否为数字和小数
        private void txtQty_KeyPress(object sender, KeyPressEventArgs e)
        {
            string str = this.txtQty.Text;
            e.Handled = e.KeyChar < '0' || e.KeyChar > '9';  //允许输入数字
            if (e.KeyChar == (char)8)  //允许输入回退键
            {
                e.Handled = false;
            }
            if (e.KeyChar == (char)46)
            {
                if (str == "")  //第一个不允许输入小数点
                {
                    e.Handled = true;
                    return;
                }
                else
                { //小数点不允许出现2次
                    foreach (char ch in str)
                    {
                        if (char.IsPunctuation(ch))
                        {
                            e.Handled = true;
                            return;
                        }
                    }
                    e.Handled = false;
                }
            }
        }
        //取消按钮
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        //当文本框验证后发生
        private void textAmount_Validated(object sender, EventArgs e)
        {
            textAmount.Text = Math.Round(float.Parse(textAmount.Text), 4).ToString();
        }
    }
}
